/**
 * @author GZY
 */
package sg.edu.nus.iss.team4ft.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import sg.edu.nus.iss.team4ft.service.User;

public class LoginIOService {

	// Returns a list of users: refer to data/user_info.txt
	public List<User> getUserLst() throws IOException, NullPointerException {
		List<User> userLst = new ArrayList<User>();
		String fileName = AppConstant.FILE_USER_INFO;

		FileInputStream fis = new FileInputStream(fileName);
		InputStreamReader inReader = new InputStreamReader(fis);
		BufferedReader bufferedReader = new BufferedReader(inReader);
		String line = bufferedReader.readLine();
		try{
			while (line != null) {
				String[] splitInput = line.split(AppConstant.FILE_DELIMITER, -1);
	
				// creates user based on 3 attributes 1.Username 2.Password 3.Role
				User user = new User(splitInput[0], splitInput[1], splitInput[2]);
				userLst.add(user);
				line = bufferedReader.readLine();
			}
		}finally{
			bufferedReader.close();
			inReader.close();
			fis.close();
		}
		return userLst;
	}
}
